package com.shop.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.mapper.ItemsMapper;
import com.shop.mapper.ItemsMapperCustom;
import com.shop.pojo.*;
import com.shop.pojo.vo.CommentLevelCountVo;
import com.shop.pojo.vo.ItemCommentVo;
import com.shop.pojo.vo.SearchItemVo;
import com.shop.pojo.vo.ShopCartVo;
import com.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2020-10-31
 */
@Service
public class ItemsServiceImpl extends ServiceImpl<ItemsMapper, Items> implements ItemsService {

    @Autowired
    private ItemsMapper itemsMapper;

    @Autowired
    private ItemsImgService itemsImgService;

    @Autowired
    private ItemsSpecService itemsSpecService;

    @Autowired
    private ItemsParamService itemsParamService;

    @Autowired
    private ItemsCommentsService itemsCommentsService;

    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    @Override
    public Items queryItemById(String itemId) {
        return itemsMapper.selectById(itemId);
    }

    @Override
    public List<ItemsImg> queryItemImgList(String itemId) {
        QueryWrapper<ItemsImg> query = new QueryWrapper<>();
        query.eq(ItemsImg.ITEM_ID,itemId);
        return itemsImgService.list(query);
    }

    @Override
    public List<ItemsSpec> queryItemSpecList(String itemId) {
        QueryWrapper<ItemsSpec> query = new QueryWrapper<>();
        query.eq(ItemsSpec.ITEM_ID,itemId);
        return itemsSpecService.list(query);
    }

    @Override
    public ItemsParam queryItemParam(String itemId) {
        QueryWrapper<ItemsParam> query = new QueryWrapper<>();
        query.eq(ItemsParam.ITEM_ID,itemId);
        return itemsParamService.getOne(query);
    }

    @Override
    public CommentLevelCountVo queryCommentCounts(String itemId) {
        Integer goodCount = getCommentCount(itemId,1);
        Integer normalCount = getCommentCount(itemId,2);
        Integer bad = getCommentCount(itemId,3);
        CommentLevelCountVo commentLevelCountVo = new CommentLevelCountVo();
        commentLevelCountVo.setBadCounts(bad);
        commentLevelCountVo.setGoodCounts(goodCount);
        commentLevelCountVo.setNormalCounts(normalCount);
        commentLevelCountVo.setTotalCounts(bad+normalCount+goodCount);
        return commentLevelCountVo;
    }

    Integer getCommentCount(String itemId,Integer level){
        QueryWrapper<ItemsComments> query = new QueryWrapper<>();
        query.eq(ItemsComments.ITEM_ID,itemId);
        query.eq(ItemsComments.COMMENT_LEVEL,level);
        return itemsCommentsService.count(query);
    }

    @Override
    public IPage<ItemCommentVo> queryPageComment(String itemId, Integer level, Page page) {
        Map map = new HashMap();
        map.put("itemId",itemId);
        map.put("level",level);
        return itemsMapperCustom.queryItemCommentsList(map,page);
    }

    @Override
    public IPage<SearchItemVo> searchItems(String keywords, String sort, Page page) {
        Map map = new HashMap();
        map.put("keywords",keywords);
        map.put("sort",sort);
        return itemsMapperCustom.searchItem(map,page);
    }

    @Override
    public IPage<SearchItemVo> searchItemByThirdCat(String catId, String sort, Page page) {
        Map map = new HashMap();
        map.put("catId",catId);
        map.put("sort",sort);
        return itemsMapperCustom.searchItemByThirdCat(map,page);
    }

    @Override
    public List<ShopCartVo> queryItemsBySpecIds(String ids) {
        String[] q = ids.split(",");
        List<String> list = Arrays.asList(q);
        return itemsMapperCustom.queryItemsBySpecIds(list);
    }
}
